package top.littlejiang.onlinexam.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.littlejiang.onlinexam.common.annotation.Log;
import top.littlejiang.onlinexam.common.base.BusinessException;
import top.littlejiang.onlinexam.common.utils.Result;
import top.littlejiang.onlinexam.entity.OlexamExam;
import top.littlejiang.onlinexam.service.OlexamExamService;
import top.littlejiang.onlinexam.service.OlexamTransService;
import top.littlejiang.onlinexam.vo.ExamAddVO;

import java.util.List;
import java.util.Map;

/**
 * @author chen
 * @Description
 * @since 2021/3/20 17:11
 */
@RequestMapping("/exam")
@Controller
public class ExamController {

    @Autowired
    OlexamExamService examService;
    @Autowired
    OlexamTransService transService;

    /**
     * 获取考试名
     * @return
     */
    @PostMapping("/getnames")
    @ResponseBody
    public Result getNames(){
        List<String> names = examService.getNames();
        return Result.ok(names);
    }

    /**
     * 获取考试列表 -》教师用
     * @param query
     * @return
     */
    @PostMapping("/getpage")
    @ResponseBody
    public Result getPages(@RequestBody OlexamExam query){
        return Result.ok(examService.getListForTeacher(query));
    }

    /**
     * 根据uuid 查询单个考试记录
     * @param query
     * @return
     */
    @PostMapping("/getone")
    @ResponseBody
    public Result getOne(@RequestBody OlexamExam query){
        if (StringUtils.isBlank(query.getExamUuid())){
            throw new BusinessException("查询条件错误！");
        }
        return Result.ok(examService.checkOne(query));
    }

    /**
     * 待考列表 -》学生用
     * @param query
     * @return
     */
    @PostMapping("/getneedcheck")
    @ResponseBody
    @RequiresPermissions("olexam:exam:needcheck")
    public Result getNeedCheck(@RequestBody OlexamExam query){
        return Result.ok(examService.getListForStu(query));
    }

    /**
     * 已考列表 -》学生用
     * @param query
     * @return
     */
    @PostMapping("/getistested")
    @ResponseBody
    @RequiresPermissions("olexam:exam:tested")
    public Result getIsTested(@RequestBody OlexamExam query){
        return Result.ok(examService.getListForStu(query));
    }

    /**
     * 检验考试名是否已存在
     * @param query
     * @return
     */
    @PostMapping("/checkname")
    @ResponseBody
    public Result checkName(@RequestBody JSONObject query){
        return Result.ok(examService.checkName(query.getString("examName")));
    }

    /**
     * 添加一次考试
     * 添加定时任务表 开考、结束考试
     * @param examAddVO
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Log("添加考试")
    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions("olexam:exam:list")
    public Result addExam(@RequestBody ExamAddVO examAddVO) throws IllegalAccessException, InstantiationException {
        transService.addExam(examAddVO);
        return Result.ok();
    }

    /**
     * 删除
     * @param uuids
     * @return
     */
    @Log("删除考试")
    @PostMapping("/delete")
    @ResponseBody
    @RequiresPermissions("olexam:exam:list")
    public Result delete(@RequestBody List<String> uuids){
        transService.deleteExam(uuids);
        return Result.ok();
    }

    /**
     * 更新
     * @param exam
     * @return
     */
    @Log("保存/更新考试")
    @PostMapping("/update")
    @ResponseBody
    @RequiresPermissions("olexam:exam:list")
    public Result update(@RequestBody OlexamExam exam){
        transService.updateExam(exam);
        return Result.ok();
    }

    /**
     * 获取考试统计结果
     * @param param
     * @return
     */
    @PostMapping("/examrst")
    @ResponseBody
    @RequiresPermissions("olexam:exam:list")
    public Result examRst(@RequestBody JSONObject param){
        String examUuid = param.getString("examUuid");
        Map<String, Object> examRst = examService.getExamRst(examUuid);
        return Result.ok(examRst);
    }
}
